"use strict";
/* Controllers */
app.controller("wubaWordsTopController", ['$scope', '$location', '$rootScope', '$routeParams', 'wubaWordTopService', function ($scope, $location, $rootScope, $routeParams, wubaWordTopService) {

    var vm = $scope.vm;
    vm.p = {};
    //vm.p.appID = $routeParams.appID; //  获取应用模块的名称


    var errorFn = function (result) {
        alert(result);
    };

    $scope.listWordTop = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        params.zpsrc = '58'
        $('#wuba_table').bootstrapTable('showLoading');
        // $('#ganji_table').bootstrapTable('showLoading');
        $("#refreshTable").addClass('disabled');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;

            $scope.areaNames = data.areaNames;
            var $resultTable = $('#wuba_table');
            if (typeof ($scope.vm.p.aggType) == "undefined" || $scope.vm.p.aggType == null || $scope.vm.p.aggType == '') {
                $resultTable.bootstrapTable('hideColumn', 'areaname');
                $resultTable.bootstrapTable('hideColumn', 'catename');
            } else if ($scope.vm.p.aggType == 'aggByAreaName') {
                $resultTable.bootstrapTable('showColumn', 'areaname');
                $resultTable.bootstrapTable('hideColumn', 'catename');
            } else if ($scope.vm.p.aggType == 'aggByCatename') {
                $resultTable.bootstrapTable('hideColumn', 'areaname');
                $resultTable.bootstrapTable('showColumn', 'catename');
            }

            $('#wuba_table').bootstrapTable('load', data.dataList);
            // $('#ganji_table').bootstrapTable('load', data.ganji);

            $('#wuba_table').bootstrapTable('hideLoading');
            // $('#ganji_table').bootstrapTable('hideLoading');

            $("#refreshTable").removeClass('disabled');


        };
        wubaWordTopService.list(params, procFn, errorFn);
    };

    $scope.clearSearch = function () {
        var vm = $scope.vm;
        vm.p.searchType = '';
        vm.p.endpoint = '';
        vm.p.areaName = '';
    };

    $scope.initializeController = function () {
        var $resultTable = $('#wuba_table');
        $resultTable.bootstrapTable({
            exportDataType: 'all',
            exportTypes: ['excel', 'json', 'xml', 'csv', 'txt'],
            pageList: [30, 100, 1000], pageSize: 30
        });

        // var $resultTable2 = $('#ganji_table');
        // $resultTable2.bootstrapTable({
        //      pageList: [30, 100,1000], pageSize: 30
        // });

        $resultTable.bootstrapTable('hideColumn', 'costHjzw');
        $resultTable.bootstrapTable('hideColumn', 'costYouxuan');
        $resultTable.bootstrapTable('hideColumn', 'resumeDeliveryNum');
        $resultTable.bootstrapTable('hideColumn', 'imNum');
        $resultTable.bootstrapTable('hideColumn', 'telConnNum');

        $resultTable.bootstrapTable('hideColumn', 'searchAs');
        $resultTable.bootstrapTable('hideColumn', 'searchEpv');
        $resultTable.bootstrapTable('hideColumn', 'searchPvr');
        $resultTable.bootstrapTable('hideColumn', 'realExposureNum');

        // $resultTable2.bootstrapTable('hideColumn', 'costHjzw');
        // $resultTable2.bootstrapTable('hideColumn', 'costYouxuan');
        // $resultTable2.bootstrapTable('hideColumn', 'resumeDeliveryNum');
        // $resultTable2.bootstrapTable('hideColumn', 'imNum');
        // $resultTable2.bootstrapTable('hideColumn', 'telConnNum');


        $scope.listWordTop();
    };

    $scope.toggleAggType = function (aggType, e) {
        $scope.vm.p.aggType = aggType;
        var isActive = $("#" + aggType).attr("class").indexOf("active")
        if (isActive >= 0) {
            $("#aggByCatename").removeClass("active")
            $("#aggByAreaName").removeClass("active")
            $("#" + aggType).removeClass("active")
            $scope.vm.p.aggType = ''
        } else {
            $("#aggByCatename").removeClass("active")
            $("#aggByAreaName").removeClass("active")
            $("#" + aggType).addClass("active")
        }


        console.log($scope.vm.p.aggType)
        // $scope.searchWordTop()
        $scope.initializeController();
        return false;
        // $scope.searchWordTop()
    }



    $scope.searchWordTop = function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listWordTop()
    };
    $scope.toggleInteval = function (interval) {
        $scope.vm.p.interval = interval;
        $scope.searchWordTop()
    }

    if ($routeParams.param != null) {
        vm.p = JSON.parse(decodeURIComponent($routeParams.param));
        if (vm.p.catename != '' && typeof (vm.p.catename) != "undefined" && vm.p.catename.indexOf('|') >= 0) {
            vm.p.catename = vm.p.catename.replaceAll('|', '/')
        }
        $scope.toggleAggType(vm.p.aggType)
        // $scope.searchWordChart()
    } else {
        $scope.initializeController();
    }




}]);

// function toThousands(num, row, index) {
//     var result = '', counter = 0;
//     num = (num || 0).toString();
//     for (var i = num.length - 1; i >= 0; i--) {
//         counter++;
//         result = num.charAt(i) + result;
//         if (!(counter % 3) && i != 0) { result = ',' + result; }
//     }
//     return result;
// }


/* Services */
app.service('wubaWordTopService', ['ajaxService', function (ajaxService) {

    this.list = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/wordTop/listByParam.do", successFunction, errorFunction);
    };

}]);

// function wubaWordFormat(value, row, index) {
//     return wordFormat(value, 'wuba')
// }

// // function ganjiWordFormat(value, row, index) {
// //     return wordFormat(value, 'ganji')
// // }

// function wordFormat(value, business) {
//     if (value == null) {
//         return "-";
//     }
//     //通过controller来获取Angular应用
//     var appElement = document.querySelector('[ng-controller=index]');
//     //获取$scope变量
//     var $scope = angular.element(appElement).scope();
//     $scope.vm.p.business = business;
//     $scope.vm.p.inword = value;

//     let urlParam = $scope.vm.p
//     if (urlParam.catename != '' && typeof (urlParam.catename) != "undefined" && urlParam.catename.indexOf("/") >= 0) {
//         urlParam.catename = urlParam.catename.replaceAll('/', '|')
//         console.log(urlParam)
//     }
//     // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'    
//     var str = '<a href="#/star/main/words_chart/' + encodeURIComponent(JSON.stringify(urlParam)) + '" target="_blank">' + value + '</a>';
//     return str;

// }

function rnkFormatter(value, row, index) {
    if (value == null) {
        return "新晋TOP词汇";
    }
    // //通过controller来获取Angular应用
    // var appElement = document.querySelector('[ng-controller=index]');
    // //获取$scope变量
    // var $scope = angular.element(appElement).scope();
    // $scope.vm.p.business = business;
    // $scope.vm.p.inword = value;

    // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'   
    if (value > 0) {
        var str = Math.abs(value) + '<span style="color:green" class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>';
        return str;
    } else if (value < 0) {
        var str = Math.abs(value) + '<span style="color:red" class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>';
        return str;
    } else {
        return value
    }

    // var str = '<span class="glyphicon glyphicon-search" aria-hidden="true"></span>搜索';
    // return str;
}

// /** 
//  * param 将要转为URL参数字符串x的对象 
//  * key URL参数字符串的前缀 
//  * encode true/false 是否进行URL编码,默认为true 
//  *  
//  * return URL参数字符串 
//  */
// var urlEncode = function (param, key, encode) {
//     if (param == null) return '';
//     var paramStr = '';
//     var t = typeof (param);
//     if (t == 'string' || t == 'number' || t == 'boolean') {
//         paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
//     } else {
//         for (var i in param) {
//             var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
//             paramStr += urlEncode(param[i], k, encode);
//         }
//     }
//     return paramStr;
// };




